package com.xujc.dao.imp;

import com.xujc.dao.IUserDAO;
import com.xujc.entity.UserEntity;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.springframework.orm.hibernate4.support.HibernateDaoSupport;

import java.util.List;

/**
 * Created by xujc on 6/16/15.
 */
public class UserDAO extends HibernateDaoSupport implements IUserDAO {

    @Override
    public UserEntity getByEmail(String email) {
        Session session = getSessionFactory().openSession();
        Query query = session.createQuery("from UserEntity where email= ?");
        query.setParameter(0,email);
        List list = query.list();
        session.close();
        return (UserEntity) list.get(0);
    }

    @Override
    public UserEntity getById(int uid) {
        Session session = getSessionFactory().openSession();
        Query query = session.createQuery("from UserEntity where UId= ?");
        query.setParameter(0,uid);
        List list = query.list();
        session.close();
        return (UserEntity) list.get(0);
    }

    @Override
    public boolean hasUser(String email) {
        Session session = getSessionFactory().openSession();
        Query query = session.createQuery("from UserEntity where email= ?");
        query.setParameter(0,email);
        List list = query.list();
        session.close();
        return list.size()>0;
    }

    @Override
    public boolean checkPassword(String email, String password) {
        Session session = getSessionFactory().openSession();
        Query query = session.createQuery("from UserEntity where email= ?");
        query.setParameter(0,email);
        List list = query.list();
        UserEntity userEntity = (UserEntity) list.get(0);
        boolean equals = userEntity.getPassword().equals(password);
        session.close();
        return equals;
    }

    @Override
    public void changePassword(int uid, String newPassword) {
        Session session = getSessionFactory().openSession();
        Transaction transaction = session.beginTransaction();
        Query query = session.createQuery("from UserEntity where UId= ?");
        query.setParameter(0,uid);
        List list = query.list();
        UserEntity userEntity = (UserEntity) list.get(0);
        userEntity.setPassword(newPassword);
        session.update(userEntity);
        transaction.commit();
        session.close();
    }

    @Override
    public void changeName(int uid, String newName) {
        Session session = getSessionFactory().openSession();
        Transaction transaction = session.beginTransaction();
        Query query = session.createQuery("from UserEntity where UId= ?");
        query.setParameter(0,uid);
        List list = query.list();
        UserEntity userEntity = (UserEntity) list.get(0);
        userEntity.setUserName(newName);
        session.update(userEntity);
        transaction.commit();
        session.close();
    }

    @Override
    public int register(String email, String password, String userName) {
        Session session = getSessionFactory().openSession();
        Transaction transaction = session.beginTransaction();
        UserEntity entity=new UserEntity();
        entity.setEmail(email);
        entity.setPassword(password);
        entity.setUserName(userName);
        session.save(entity);
        transaction.commit();
        session.close();

        return entity.getUId();
    }
}
